//
//  BKSignGoodsCollectionViewCell.m
//  bestkeep
//
//  Created by yons on 16/12/5.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKSignGoodsCollectionViewCell.h"
#import "BKMainCommon.h"
#import "BK_OD_Color.h"



@implementation BKSignGoodsCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self initView];
    }
    return self;
}

-(void)initView{
    
    self.backgroundColor=[UIColor whiteColor];
    
    _imgV_Goods=[[UIImageView alloc]init];
    _imgV_Goods.backgroundColor=[UIColor clearColor];
    _imgV_Goods.image=[UIImage imageNamed:@"pic_newGoods"];
    [self addSubview:_imgV_Goods];
    
    
    _lab_name=[[UILabel alloc]init];
    _lab_name.backgroundColor=[UIColor clearColor];
    _lab_name.text=@"加载中...";
    _lab_name.numberOfLines=0;
    _lab_name.font =[UIFont systemFontOfSize:13.0];
    _lab_name.textColor=kOrderBlack1;
    _lab_name.textAlignment=NSTextAlignmentLeft;
    [self addSubview:_lab_name];
    
    
    
    _lab_number=[[UILabel alloc]init];
    _lab_number.backgroundColor=[UIColor clearColor];
    _lab_number.text=@"加载中...";
    _lab_number.font =[UIFont systemFontOfSize:14.0];
    _lab_number.textColor=kOrderBlack1;
    _lab_number.textAlignment=NSTextAlignmentLeft;
    [self addSubview:_lab_number];
    
    _lab_price=[[UILabel alloc]init];
    _lab_price.backgroundColor=[UIColor clearColor];
    _lab_price.text=@"加载中...";
    _lab_price.font =[UIFont systemFontOfSize:12.0];
    _lab_price.textColor=RGBACOLOR(136, 136, 136, 1);
    _lab_price.textAlignment=NSTextAlignmentLeft;
    [self addSubview:_lab_price];
    
    
    
    
    _lab_exchange=[[UILabel alloc]init];
    _lab_exchange.backgroundColor=RGBACOLOR(0, 186, 156, 1);
    _lab_exchange.text=@"去兑换";
    _lab_exchange.font =[UIFont systemFontOfSize:15.0];
    _lab_exchange.textColor=[UIColor whiteColor];
    _lab_exchange.textAlignment=NSTextAlignmentCenter;
    [self addSubview:_lab_exchange];
    
    
    /*
     * 商品图片
     */
    //    _imgV_Goods.backgroundColor=[UIColor yellowColor];
    [_imgV_Goods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).mas_offset(25/2.0);
        make.centerX.equalTo(self.mas_centerX);
        make.height.mas_offset(230/2.0);
        make.width.mas_offset(230/2.0);
    }];
    /*
     * 商品名字
     */
    //    _lab_name.backgroundColor=[UIColor  redColor];
    [_lab_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_imgV_Goods.mas_bottom).mas_offset(8);
        make.right.equalTo(self.mas_right).mas_offset(-10);
        make.left.equalTo(self.mas_left).mas_offset(10);
        make.height.mas_offset(40);
    }];
    
    
    /*
     * _lab_exchange
     */
    [_lab_exchange mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).mas_offset(0);
        make.centerX.equalTo(self.mas_centerX);
        make.height.mas_offset(70/2.0);
        //        make.width.mas_offset(260/2.0);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
    }];
    
    /*
     * 积分
     */
    [_lab_number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).mas_offset(10);
        make.bottom.equalTo(_lab_exchange.mas_top).mas_offset(-8);
    }];
    
    /*
     * 价格
     */
    [_lab_price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).mas_offset(-10);
        make.left.equalTo(_lab_number.mas_right).mas_offset(10);
        make.bottom.equalTo(_lab_number.mas_bottom);
    }];
    
    
    _view_line=[[UIView alloc]init];
    _view_line.backgroundColor=RGBACOLOR(136, 136, 136, 1);
    [self addSubview:_view_line];
    
    
    _btn_mask=[[UIButton alloc]init];
    [self addSubview:_btn_mask];
    _btn_mask.backgroundColor=[UIColor  clearColor];
    [_btn_mask mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(-45);
        make.top.equalTo(self.mas_top).offset(0);
        make.right.equalTo(self.mas_right).offset(0);
    }];
    
    
}

+(CGSize)setSize:(id)data{
    if (!data) {
        return CGSizeZero;
    }else{
        return CGSizeMake((ScreenWidth-20.0f)/2.0-5.0f,473/2.0);
    }
}

-(void)relayOutUIWithModel:(BKSignGoodsModel*)model{
    if (model) {

        
        [_imgV_Goods load:model.goodsImg placeholderImage:[Resources imageNamed:@"BKInteral_GoodsDefault"]];
        if ([[NSString stringWithFormat:@"%@",model.goodsName]  boundingRectWithSize:CGSizeMake((ScreenWidth-20.0f)/2.0-5.0f-20, 38) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height>2*13) {
            _lab_name.text = model.goodsName;
        }else{
            _lab_name.text = [NSString stringWithFormat:@"%@\n",model.goodsName];
        }
        if ([[NSString stringWithFormat:@"%@",model.exchangeFlag] integerValue]==1) {
            _lab_exchange.text=@"去兑换";
            _lab_exchange.backgroundColor=RGB(0, 186, 156);
            [_btn_mask mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.mas_left).offset(0);
                make.bottom.equalTo(self.mas_bottom).offset(-473/2.0);
                make.top.equalTo(self.mas_top).offset(0);
                make.right.equalTo(self.mas_right).offset(0);
            }];
        }else if([[NSString stringWithFormat:@"%@",model.exchangeFlag] integerValue]==2){
            _lab_exchange.text=@"今日已兑换";
            _lab_exchange.backgroundColor=RGB(178, 178, 178);
            [_btn_mask mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.mas_left).offset(0);
                make.bottom.equalTo(self.mas_bottom).offset(-473/2.0);
                make.top.equalTo(self.mas_top).offset(0);
                make.right.equalTo(self.mas_right).offset(0);
            }];
        }else {
            _lab_exchange.text=@"已抢完";
            _lab_exchange.backgroundColor=RGB(178, 178, 178);
            [_btn_mask mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.mas_left).offset(0);
                make.bottom.equalTo(self.mas_bottom).offset(-473/2.0);
                //            make.bottom.equalTo(self.mas_bottom).offset(0);
                make.top.equalTo(self.mas_top).offset(0);
                make.right.equalTo(self.mas_right).offset(0);
            }];
        }
        
        
        /*
         * 还需要自己 加的价格
         */
        NSString * str_needMoney;
        
        
        /*
         * 积分
         */
        if (model.needMoney.length>1) {
            str_needMoney=[NSString stringWithFormat:@"%@",model.needMoney];
            //        _lab_number.text=[NSString stringWithFormat:@"%@+%@",model.needIntegral,str_needMoney];
            
            
            NSMutableAttributedString *richText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@+%@",model.needIntegral,str_needMoney]];
            
            
            //        [richText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16.0] range:NSMakeRange(0, leght)];//设置字体大小
            
            
            [richText addAttribute:NSForegroundColorAttributeName value:RGB(255, 16, 16) range:NSMakeRange([NSString stringWithFormat:@"%@+",model.needIntegral].length,str_needMoney.length)];//设置字体颜色
            
            _lab_number.attributedText=richText;
            
            
            _lab_price.hidden=YES;
            _view_line.hidden=YES;
        }else{
            _lab_number.text=[NSString stringWithFormat:@"%@",model.needIntegral];
            _lab_price.hidden=NO;
            _view_line.hidden=NO;
        }
        
        
        
        /*
         * 价格
         */
        _lab_price.text=[NSString stringWithFormat:@"%@",model.goodsPrice];
        
        
        /*
         * 积分
         */
        [_lab_number mas_remakeConstraints:^(MASConstraintMaker *make) {
            if ((ScreenWidth-20.0f)/2.0-5.0f-20>=[BKIntegralCommon getWidthWithTitle:_lab_number.text font:14.0]) {
                make.width.mas_equalTo([BKIntegralCommon getWidthWithTitle:_lab_number.text font:14.0]);
            }else{
                make.width.mas_equalTo((ScreenWidth-20.0f)/2.0-5.0f-20);
            }
            make.left.equalTo(self.mas_left).mas_offset(10);
            make.bottom.equalTo(_lab_exchange.mas_top).mas_offset(-8);
        }];
        
        
        
        /*
         * 价格
         */
        [_lab_price mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_lab_number.mas_right).mas_offset(10);
            make.bottom.equalTo(_lab_number.mas_bottom);
            
            NSLog(@"-----------------66666-------%@-------%f--------%f",_lab_number.text,[BKIntegralCommon getWidthWithTitle:_lab_number.text font:14.0],(ScreenWidth-20.0f)/2.0-5.0f-20);
            
            
            if ((ScreenWidth-20.0f)/2.0-5.0f-20-[BKIntegralCommon getWidthWithTitle:_lab_number.text font:14.0]>=[BKIntegralCommon getWidthWithTitle:_lab_price.text font:12.0]) {
                make.width.mas_equalTo([BKIntegralCommon getWidthWithTitle:_lab_price.text font:12.0]);
            }else{
                //           make.right.equalTo(self.mas_right).mas_offset(-10);
                
                CGFloat Width=(ScreenWidth-20.0f)/2.0-5.0f-20-[BKIntegralCommon getWidthWithTitle:_lab_number.text font:14.0] -10;
                
                make.width.mas_equalTo(Width);
            }
            
            
        }];
        
        /*
         * 删除线
         */
        [_view_line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_lab_price.mas_right);
            make.left.equalTo(_lab_price.mas_left);
            make.centerY.equalTo(_lab_price.mas_centerY);
            make.height.mas_equalTo(0.5);
        }];
    }
}
@end
